#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan Zip View Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_45"
__date__    = "2014/05/04"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import os
import traceback
from zipfile import ZipFile, ZIP_DEFLATED
import tempfile

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.core import MIKAN_HTTP_HEADER
from mikan.view import MikanBinaryView

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan Contant
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanZipView(MikanBinaryView):

	def __init__(self, r, filename, files, password=None):
		super(MikanZipView, self).__init__(r)

		self.FPassword = password
		self.FFilename = filename
		self.FFiles = files

	def get_password(self):
		return self.FPassword

	def set_password(self, value):
		self.FPassword = value

	def get_files(self):
		return self.FFiles

	def get_filename(self):
		return self.FFilename

	def set_filename(self, value):
		self.FFilename = value

	def get_content_type(self):
		return "application/x-compress"

	def perform(self):
		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % (self.filename)

		# Zipファイル名
		filename = tempfile.mktemp()

		# Zipファイル作成
		zip = ZipFile(filename, 'w', ZIP_DEFLATED)

		try:
			# Zipファイル格納
			for f,n in self.files.items():
#				zip.write(f.encode('mbcs'), n.encode('mbcs'))
				zip.write(f.encode('utf-8'), n.encode('utf-8'))
		finally:
			try:
				# Zip閉じる
				zip.close()
			except:
				pass

		# Zipファイル読み込み
		f = None
		try:
			f = open(filename, 'rb')
			self.FContent = f.read()
		except:
			self.logger.debug(traceback.format_exc())
			self.FContent = ""
		finally:
			try:
				if f:
					f.close()
			except:
				pass

		# 削除
		try:
			print filename
			os.remove(filename)
		except:
			pass

		return True

	# ----------------------------------------------------
	## Property
	filename = ESiPCProperty(get_filename, set_filename, None)
	password = ESiPCProperty(get_password, set_password, None)
	files = ESiPCProperty(get_files, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------

# ------------------------------------------------------------
